package com.runfast.waimai.web.controller.Creditlevel;

import com.runfast.paotui.dao.model.RunfastAgentaccounts;
import com.runfast.paotui.dao.model.RunfastAgentbusiness;
import com.runfast.paotui.service.RunfastAgentaccountsService;
import com.runfast.paotui.service.RunfastAgentbusinessService;
import com.runfast.waimai.dao.model.RunfastCreditlevel;
import com.runfast.waimai.service.RunfastCreditlevelService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * ${DESCRIPTION} 骑手
 *
 * @author mengxiang
 * @create 2018-05-17 17:59
 **/
@Controller
@RequestMapping("/server/shopper/*")
public class ShopperRunfastCreditlevelContrllor {
    @Resource
    private RunfastAgentaccountsService runfastAgentaccountsService;
    @Resource
    private RunfastAgentbusinessService runfastAgentbusinessService;
    @Resource
    RunfastCreditlevelService service; //骑手信用等级管理
    @PostMapping("updatecreditlevel")
    public @ResponseBody ModelMap  updatecreditlevel(@RequestBody RunfastCreditlevel creditlevel){
        ModelMap model=new ModelMap();
     if(creditlevel !=null){   service.updateByPrimaryKeySelective(creditlevel);
        model.put("success",true);
         model.put("msg","修改成功");
     } else {
         model.put("success",false);
         model.put("msg","修改不成功");
     }

        return model;
    }
    @PostMapping("addcreditlevel")
    public @ResponseBody ModelMap  addcreditlevel(@RequestBody RunfastCreditlevel creditlevel){
        System.out.println(creditlevel+"#############################");

        ModelMap model=new ModelMap();
        if(creditlevel !=null ){
            RunfastAgentbusiness runfastAgentaccounts=runfastAgentbusinessService.selectByPrimaryKey(creditlevel.getAgentid());
            System.out.println(runfastAgentaccounts+"$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
            creditlevel.setCreatetime(new Date());

            creditlevel.setAgentname(runfastAgentaccounts.getName());

            service.insert(creditlevel);
            model.put("success",true);
            model.put("msg","添加成功");
        }else {
            model.put("success",false);
            model.put("msg","添加不成功");
        }


        return model;
    }
    @PostMapping("delcreditlevel")
    public @ResponseBody ModelMap  delcreditlevel(@RequestBody RunfastCreditlevel creditlevel){ //信誉等级管理id
        ModelMap model=new ModelMap();
        if(creditlevel.getId() !=null){   service.delete(creditlevel.getId());
            model.put("success",true);

            model.put("msg","删除成功");
        }else {
            model.put("msg","删除不成功");
            model.put("success",false);
        }

        return model;
    }
    @GetMapping("findcreditlevel")
    public @ResponseBody ModelMap  findcreditlevel(@RequestParam(value = "sort") Integer sort,@RequestParam(value = "agentIds") String agentIds, @RequestParam(value = "page",required = false,defaultValue = "0") Integer page,@RequestParam(value = "pagesize",required = false,defaultValue = "10") Integer pagesize){ //信誉等级管理id
        ModelMap model=new ModelMap();
        Integer page1=page*pagesize;
        System.out.println(sort+"##"+agentIds);
        if(agentIds.equals("")||agentIds==null) {

            List<RunfastCreditlevel> creditlevels=service.findEndgrade(null,sort,page1,pagesize);
            Integer count=service.findEndgradecount(null,sort);
            System.out.println(123+"看看3");
            System.out.println(creditlevels);
            System.out.println(count+"看看4");
            if(creditlevels!=null){
                model.put("creditlevel",creditlevels);
                model.put("count",count);
                model.put("success",true);
            }else {
                model.put("msg","没有你要的数据");
                model.put("success",false);

            }

        }else {
            Integer agentid = Integer.parseInt(agentIds);

            System.out.println(agentIds + "看看1");
            System.out.println(sort + "看看2");

            List<RunfastCreditlevel> creditlevels = service.findEndgrade(agentid, sort, page1, pagesize);
            Integer count = service.findEndgradecount(agentid, sort);
            System.out.println(123+"看看5");
            System.out.println(creditlevels);
            System.out.println(count+"看看6");
            if (creditlevels != null) {
                model.put("creditlevel", creditlevels);
                model.put("count", count);
                model.put("success", true);
            } else {
                model.put("msg", "没有你要的数据");
                model.put("success", false);

            }
        }

        return model;
    }
    @PostMapping("findcreditlevelAll")
    public @ResponseBody ModelMap  findcreditlevelAll(){ //信誉等级管理id
        ModelMap model=new ModelMap();
       List<RunfastCreditlevel> creditlevel=service.findAll();
        if(creditlevel!=null){
           model.put("creditlevel",creditlevel);
            model.put("success", true);
        } else {
            model.put("msg","没有你要的数据");
            model.put("success",false);
        }

        return model;
    }
    @PostMapping("findcreditlevelAndAgentid")//
    public @ResponseBody ModelMap  ifAndDir(@RequestBody RunfastCreditlevel creditleve1){ //信誉等级管理id 和代理商ID 查询
        ModelMap model=new ModelMap();
        List<RunfastCreditlevel> creditlevels=service.findcreditlevelAndAgentid(creditleve1.getEndgrade(),creditleve1.getAgentname());
        if(creditlevels!=null){
            model.put("creditlevels",creditlevels);
            model.put("success", true);
        } else {
            model.put("msg","没有你要的数据");
            model.put("success",false);
        }

        return model;
    }
}
